home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / win / c / winet.com / MACROS.M < prev    next >
Encoding:
Text File  |  1988-02-04  |  5.0 KB  |  228 lines

  1. ;
  2. ; This file contains some macro definitions.
  3. ;
  4.             include  utils.mac
  5.             .xlist
  6. .xcref ?ASET,?AVAL,?PUSH,?POP,?TOS,?J,?JNF,?LBL,?mx
  7. .xcref ?if,?while,?forever
  8. .xcref ?brkcnt,?brklab
  9. .xcref ?dbsp,?STK0,?STKT0,?STKTOPT,?STKTOP,?CTR
  10. ?mx MACRO a,b,c,d,e,f ;; Macro to allow multi statements on line
  11.  a
  12.  b
  13.  c
  14.  d
  15.  e
  16.  f
  17. ENDM
  18. ?mx <?if equ 1>,<?while equ 2>,<?forever equ 3> ; Declare stack types
  19. ?repeat equ 4
  20. ?mx ?brkcnt=-1,?brklab=-1                       ; Useful variables for BREAK
  21. ?mx IF2,<.ERRNZ ?dbsp ;; Nesting error>,ENDIF     ; Check for nesting errors:
  22. ; Initialise variables for stack handling:
  23. ?mx ?dbsp=0,?STK0=0,?STKT0=0,?STKTOP=-1,?STKTOPT=-1,?CTR=0
  24.  
  25. ?ASET MACRO v,l,n
  26.  .xcref v&l
  27.  v&l = n ;; Set an 'array' variable.
  28. ENDM
  29.  
  30. ?AVAL MACRO n,v,l
  31.  n = v&l ;; Read an 'array' variable.
  32. ENDM
  33.  
  34. ?PUSH MACRO n,t ;; Push new value onto stack.
  35. ?mx ?dbsp=?dbsp+1,?STKTOP=n,?STKTOPT=t
  36.  ?ASET ?STK,%?dbsp,n
  37.  ?ASET ?STKT,%?dbsp,t
  38. ENDM
  39.  
  40. ?POP MACRO ;; Pop top element off stack.
  41. ?mx <IF ?dbsp GT 0>,?dbsp=?dbsp-1,ENDIF
  42.    ?AVAL    ?STKTOP,?STK,%?dbsp
  43.    ?AVAL    ?STKTOPT,?STKT,%?dbsp
  44. ENDM
  45.  
  46. ?TOS  MACRO t        ;; Check type of top of stack.
  47. .ERRE ?dbsp ; Stack underflow
  48. .ERRNZ ?STKTOPT NE t ; Stack type mismatch
  49. ENDM
  50.  
  51. ?JNF MACRO c,t,n ;; Produce far conditional jump - beware too many n's.
  52.  local L
  53. .xcref L
  54.             j&c L
  55.             jmp t&n
  56. L LABEL NEAR
  57. ENDM
  58.  
  59. ?J MACRO c,t,n  ;; Jump conditional to t&n
  60.             j&c t&n
  61. ENDM
  62.  
  63. ?LBL  MACRO t,n   ;; Label t&n
  64.    .xcref   t&n
  65.    IFNDEF   t&n
  66.       t&n LABEL NEAR
  67.    ELSE
  68.       IF t&n EQ $
  69. t&n LABEL NEAR
  70.       ENDIF
  71.    ENDIF
  72. ENDM
  73.  
  74. ;
  75. ; Now I have some stack handling here are the REAL macros.
  76. ;
  77. .IF MACRO f,x,y
  78.    IFNB <f>
  79.     IFDIF <f>,<far>
  80.      IFDIF <f>,<FAR>
  81.       IFDIF <f>,<Far>
  82.        .ERRNB <y> ;; Too many args to .IF
  83.        .IF <>,<f>,<x>
  84.        EXITM
  85.       ENDIF
  86.      ENDIF
  87.     ENDIF
  88.    ENDIF
  89.    ?PUSH ?CTR,?if
  90.    ?CTR=?CTR+1
  91.    IFB   <y>   ;; Only 1 parm - must be condition.
  92.       IFB <f>
  93.          ?J n&x,?E,%?STKTOP
  94.       ELSE ;; Far jump on not x
  95.          ?JNF <x>,?E,%?STKTOP
  96.       ENDIF
  97.    ELSE
  98.    ;; Expand x into a list of instructions:
  99. ?mx x
  100.       IFB <f>
  101.          ?J n&y,?E,%?STKTOP
  102.       ELSE
  103.          ?JNF <y>,?E,%?STKTOP
  104.       ENDIF
  105.    ENDIF
  106. ENDM
  107. .ELSE MACRO    ;; Doesn't need a far option.
  108.    ?TOS  ?if
  109.    ?J    <>,?F,%?STKTOP
  110.    ?LBL  ?E,%?STKTOP
  111. ENDM
  112. .FI   MACRO
  113.    ?TOS  ?if
  114.    ?LBL  ?E,%?STKTOP
  115.    ?LBL  ?F,%?STKTOP
  116.    ?POP
  117. ENDM
  118.  
  119. ; All loops have a repeat label ?R... at the top and a break ?B... at the foot
  120.  
  121. .FOREVER MACRO d   ;; Infinite loop - ignores parameter
  122.    .ERRDIF <d>,<DO>  ; Keyword DO expected
  123.    ?PUSH ?CTR,?forever
  124.    ?CTR = ?CTR+1
  125.    ?LBL  ?R,%?STKTOP
  126. ENDM
  127.  
  128. .REPEAT MACRO        ;; Condition at bottom - no parameters here.
  129.    ?PUSH ?CTR,?repeat
  130.    ?CTR = ?CTR+1
  131.    ?LBL  ?R,%?STKTOP
  132. ENDM
  133.  
  134. ; .UNTIL cc  is just: BREAK Ncc inside an infinite loop:
  135. .UNTIL MACRO x
  136.    .ERRB <x> ; Condition expected .UNTIL
  137.  
  138.    IF ?STKTOPT EQ ?repeat
  139.       IFB <f>
  140.          ?J n&x,?R,%?STKTOP
  141.       ELSE
  142.          ?JNF x,?R,%?STKTOP
  143.       ENDIF
  144.       ?LBL ?B,%?STKTOP
  145.       ?POP
  146.    ELSE
  147.       .ERR Unmatched .UNTIL
  148.    ENDIF
  149. ENDM
  150.  
  151. ; WHILE cc DO is just: BREAK Ncc inside an infinite loop:
  152. .WHILE MACRO f,x,y,z
  153.    IFNB <f>
  154.     IFDIF <f>,<far>
  155.      IFDIF <f>,<FAR>
  156.       IFDIF <f>,<Far>
  157.        .ERRNB <z> ;; Too many args to .WHILE
  158.        .WHILE <>,<f>,<x>,<y>
  159.        EXITM
  160.       ENDIF
  161.      ENDIF
  162.     ENDIF
  163.    ENDIF
  164.    .ERRB <y> ; Condition or DO expected
  165.    ?PUSH ?CTR,?while
  166.    ?CTR = ?CTR+1
  167.    ?LBL  ?R,%?STKTOP
  168.    IFB <z>   ;; x is condition, y is 'DO'
  169.       .ERRDIF <y>,<DO> ; DO expected
  170.       .ERRB <x> ; No condition for .WHILE
  171.       IFB <f>
  172.          ?J n&x,?B,%?STKTOP
  173.       ELSE
  174.          ?JNF x,?B,%?STKTOP
  175.       ENDIF
  176.    ELSE
  177.       .ERRDIF <z>,<DO>  ; DO expected
  178. ?mx x
  179.       IFB <f>
  180.          ?J n&y,?B,%?STKTOP
  181.       ELSE
  182.          ?JNF y,?B,%?STKTOP
  183.       ENDIF
  184.    ENDIF
  185. ENDM
  186.  
  187. .OD MACRO ;; End of loop.
  188.    IF (?STKTOPT EQ ?forever) OR (?STKTOPT EQ ?while)
  189.       ?J <>,?R,%?STKTOP
  190.       ?LBL  ?B,%?STKTOP
  191.       ?POP
  192.    ELSE
  193.       .ERR  ; Nesting error
  194.    ENDIF
  195. ENDM
  196.  
  197. .BREAK MACRO f,cc    ;; Break from loop - this may involve searching down stack!
  198.    IFNB <f>
  199.     IFDIF <f>,<far>
  200.      IFDIF <f>,<FAR>
  201.       IFDIF <f>,<Far>
  202.        .ERRNB <cc> ;; Too many args to .BREAK
  203.        .BREAK <>,<f>
  204.        EXITM
  205.       ENDIF
  206.      ENDIF
  207.     ENDIF
  208.    ENDIF
  209.    ?brkcnt=?dbsp
  210.    ?brklab=-1
  211.    .ERRE ?dbsp   ; BREAK outside all loops
  212.    REPT ?dbsp
  213.       ?AVAL ?brklab,?STKT,%?brkcnt
  214.       IF (?brklab EQ ?forever) OR (?brklab EQ ?while) OR (?brklab EQ ?repeat)
  215.          ?AVAL ?brklab,?STK,%?brkcnt
  216.          IFB <f>
  217.             ?J <cc>,?B,%?brklab
  218.          ELSE
  219.             ?JNF n&cc,?B,%?brklab
  220.          ENDIF
  221.          EXITM
  222.       ENDIF
  223.       ?brkcnt = ?brkcnt - 1
  224.       .ERRE ?brkcnt  ; BREAK outside all loops
  225.    ENDM
  226. ENDM
  227.             qlist    ;; Restores listing.
  228.